﻿using System.Collections.Generic;
using BitButterCORE.Object;

namespace BitButterCORE.Event
{
	public abstract class BaseModifiableEvent<TEventArgs, TEvent> : BaseEvent<TEventArgs, TEvent>
		where TEventArgs : BaseModifiableEventArgs
		where TEvent : BaseModifiableEvent<TEventArgs, TEvent>
	{
		public override void RaiseEvent(IObjectReference<BaseObject> sender, TEventArgs eventArgs)
		{
			foreach (EventModifier modifier in Modifiers)
			{
				modifier.Invoke(sender, eventArgs);
			}

			eventArgs.Merge();

			base.RaiseEvent(sender, eventArgs);
		}

		List<EventModifier> Modifiers
		{
			get
			{
				if (modifiers == null)
				{
					modifiers = new List<EventModifier>();
				}
				return modifiers;
			}
		}

		List<EventModifier> modifiers;

		public delegate void EventModifier(IObjectReference<BaseObject> sender, TEventArgs e);

		public event EventModifier Modifier
		{
			add
			{
				Modifiers.Add(value);
			}
			remove
			{
				Modifiers.Remove(value);
			}
		}
	}
}
